<!doctype html>
<html class="theme-next use-motion theme-next-mist">
<head>
  
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

  <meta name="google-site-verification" content="K8DCBviaoTBKVs28YBB7IBIbospQ9RVlgSh81RYMUhY" />


  <meta name="baidu-site-verification" content="tXr3ZTm3Hx" />



  <link rel="stylesheet" type="text/css" href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5"/>


<link rel="stylesheet" type="text/css" href="/css/main.css?v=0.4.5.1"/>

  <meta name="description" content="xgfe's blog. 鲜果前端的技术博客，鲜果前端研发部官方博客。前端基础技术研究：html, html5, javascript, css, css3；前端框架研究：angularJs, react, react native." />


  <meta name="keywords" content="javascript,cocos2d-x," />


  <link rel="alternate" target="_blank" href="/atom.xml" title="xgfe" type="application/atom+xml" />


  <link rel="shorticon icon" type="image/x-icon" href="http://p0.meituan.net/xgfe/2db359f56ce13be30dedef160e0e57ce16958.ico?v=0.4.5.1" />

<meta name="description" content="cocos2d-x是一个广泛流行的2D跨平台游戏引擎。采用c++作为开发语言。而cocos2d-Js是cocos2d-x采用脚本绑定技术实现的采用JavaScript语言作为开发语言的版本。 本博客主要内容分为三部分：  环境搭建 核心概念 相关扩展">
<meta name="keywords" content="javascript,cocos2d-x">
<meta property="og:type" content="article">
<meta property="og:title" content="cocos2d-Js 基础入门">
<meta property="og:url" content="http://xgfe.github.io/2017/05/15/jakii/cocos2d-x introduction/index.html">
<meta property="og:site_name" content="xgfe">
<meta property="og:description" content="cocos2d-x是一个广泛流行的2D跨平台游戏引擎。采用c++作为开发语言。而cocos2d-Js是cocos2d-x采用脚本绑定技术实现的采用JavaScript语言作为开发语言的版本。 本博客主要内容分为三部分：  环境搭建 核心概念 相关扩展">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948390188781.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14944929318503.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14944957996388.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945711804166.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945723879457.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945729564081.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945743415606.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948319177139.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948325815436.jpg">
<meta property="og:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948330435333.jpg">
<meta property="og:updated_time" content="2017-07-06T09:59:23.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="cocos2d-Js 基础入门">
<meta name="twitter:description" content="cocos2d-x是一个广泛流行的2D跨平台游戏引擎。采用c++作为开发语言。而cocos2d-Js是cocos2d-x采用脚本绑定技术实现的采用JavaScript语言作为开发语言的版本。 本博客主要内容分为三部分：  环境搭建 核心概念 相关扩展">
<meta name="twitter:image" content="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948390188781.jpg">


<script type="text/javascript" id="hexo.configuration">
  var CONFIG = {
    scheme: 'Mist',
    sidebar: 'post'
  };
</script>

  <title> cocos2d-Js 基础入门 | xgfe </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  <div style="position: fixed; top: -9999px; left: -9999px;">
    <img src="http://p0.meituan.net/xgfe/082a9624ba5ae8602150a2d43968463e49348.png" alt="xgfe"/>
  </div>
  <!--[if lte IE 8]>
  <div style=' clear: both; height: 59px; padding:0 0 0 15px; position: relative;margin:0 auto;'>
    <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
      <img src="http://7u2nvr.com1.z0.glb.clouddn.com/picouterie.jpg" border="0" height="42" width="820"
           alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today or use other browser ,like chrome firefox safari."
           style='margin-left:auto;margin-right:auto;display: block;'/>
    </a>
  </div>
<![endif]-->
  

  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?3601d4483819a5ab6ddabb0b6422a328";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <div class="container one-column page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><h1 class="site-meta">
  <span class="logo-line-before"><i></i></span>
  <a href="/" class="brand" rel="start">
      <span class="logo">
        <i class="icon-next-logo"></i>
      </span>
      <span class="site-title">xgfe</span>
  </a>
  <span class="logo-line-after"><i></i></span>
</h1>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  
  

  
    <ul id="menu" class="menu menu-left">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            <i class="menu-item-icon icon-next-home"></i> <br />
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            <i class="menu-item-icon icon-next-archives"></i> <br />
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            <i class="menu-item-icon icon-next-tags"></i> <br />
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-join">
          <a href="/join" rel="section">
            <i class="menu-item-icon icon-next-join"></i> <br />
            加入我们
          </a>
        </li>
      
      <!-- slide-links added by felix -->
      <li class="menu-item menu-item-slides" style="opacity: 1; transform: translateY(0px);">
        <a href="http://xgfe.github.io/Basics/" target="_blank" rel="section">
          <i class="menu-item-icon icon-next-slides"></i> <br>
          Basics
        </a>
      </li>
      <li class="menu-item menu-item-slides" style="opacity: 1; transform: translateY(0px);">
        <a href="https://slides.com/xgfe" target="_blank" rel="section">
          <i class="menu-item-icon icon-next-slides"></i> <br>
          Slides
        </a>
      </li>

      
      
    </ul>
  

  
    <div class="site-search">
      

    </div>
  

    <div class="site-search">
      <form class="site-search-form" id="gg-form" action="https://www.google.com/webhp" >
        <input type="text" name="q" id="gg-search-input" class="menu-search-input">
      </form>
    </div>
</nav>
 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div id="content" class="content"> 

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">

      
      
        <h1 class="post-title" itemprop="name headline">
          
          
            
              cocos2d-Js 基础入门
            
          
        </h1>
      

      <div class="post-meta">
        <span class="post-time">
          发表于
          <time itemprop="dateCreated" datetime="2017-05-15T16:30:00+08:00" content="2017-05-15">
            2017-05-15
          </time>
        </span>

        
          <span class="post-category" >
            &nbsp; | &nbsp; 作者
            
              <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                <a href="/categories/jakii/" itemprop="url" rel="index">
                  <span itemprop="name">jakii</span>
                </a>
              </span>

              
              

            
          </span>
        

        
          
        

        <!-- tags 挪动位置 -->
        
          <span class="post-tags">
            &nbsp; | &nbsp;
            
              <a href="/tags/javascript/" rel="tag"><i class="icon-next-tags"></i>javascript</a>
            
              <a href="/tags/cocos2d-x/" rel="tag"><i class="icon-next-tags"></i>cocos2d-x</a>
            
          </span>
        
      </div>
    </header>

    <div class="post-body">

      
      

      
        <span itemprop="articleBody"><p><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948390188781.jpg" alt=""></p>
<p>cocos2d-x是一个广泛流行的2D跨平台游戏引擎。采用c++作为开发语言。而cocos2d-Js是cocos2d-x采用脚本绑定技术实现的采用JavaScript语言作为开发语言的版本。</p>
<p>本博客主要内容分为三部分：</p>
<ul>
<li>环境搭建</li>
<li>核心概念</li>
<li>相关扩展</li>
</ul>
<a id="more"></a>
<h2 id="环境搭建"><a href="#环境搭建" class="headerlink" title="环境搭建"></a>环境搭建</h2><h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><p>cocos2d-x是一个广泛流行的2D跨平台游戏引擎。采用c++作为开发语言。而cocos2d-Js是cocos2d-x采用脚本绑定技术实现的采用JavaScript语言作为开发语言的版本。</p>
<h3 id="设计目标"><a href="#设计目标" class="headerlink" title="设计目标"></a>设计目标</h3><p><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14944929318503.jpg" alt=""></p>
<p>cocos2d-x的设计目标是横向兼容各个不同的平台，包括多种手机平台和pc平台。纵向支持c++、js、lua多种语言<br>编写代码，而且支持canvas，openGl渲染等</p>
<h3 id="开发环境搭建"><a href="#开发环境搭建" class="headerlink" title="开发环境搭建"></a>开发环境搭建</h3><ul>
<li>下载安装webstorm，作为开发IDE</li>
<li>安装chrome插件jetBrains IDE Support</li>
<li>下载cocos官方核心包</li>
<li>配置cocos2d-x环境<ul>
<li>安装python2.x版本</li>
<li>安装Apache Ant工具</li>
<li>安装其它诸如安卓sdk等特定环境的sdk等</li>
<li>运行python setup安装cocos工具</li>
</ul>
</li>
</ul>
<p>按照系统提示把上面步骤进行完成后，就可以进行项目的开发了</p>
<h3 id="一个简单的实例-helloWorld"><a href="#一个简单的实例-helloWorld" class="headerlink" title="一个简单的实例 helloWorld"></a>一个简单的实例 helloWorld</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">创建项目:cocos <span class="keyword">new</span> helloWorld -l js</div><div class="line">运行项目:cocos run -p web|ios|android|mac|win32</div><div class="line">打包:cocos compile -p web|ios|android|mac|win32 -m release</div></pre></td></tr></table></figure>
<p>创建生成的目录如下<br><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14944957996388.jpg" alt=""><br>frameworks是cocos2d-x的源文件，我们使用的一些库和API都是里面定义的。<br>res目录存放我们的资源文件，包括图片、音频、字体等资源。<br>src目录主要存放我们的代码，是我们主要的工作区域，我们的代码逻辑和资源引入都在此处进行。<br>index.html文件主要是用于web端的主页面，main.js主要是游戏启动，以及一些对页面的设置问题。<br>project.json主要是一些项目设置，以及js文件的引入</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//在index.html中,由于浏览器的兼容性问题，采用canvas来渲染，此处定义屏幕大小</span></div><div class="line">&lt;canvas id=<span class="string">"gameCanvas"</span> width=<span class="string">"480"</span> height=<span class="string">"720"</span>&gt;<span class="xml"><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span></div><div class="line"></div><div class="line"><span class="comment">//在main.js中，此处代码用来加载游戏资源，同时用导演对象启动游戏</span></div><div class="line">cc.LoaderScene.preload(g_resources, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</div><div class="line">    cc.director.runScene(<span class="keyword">new</span> HelloWorldScene());</div><div class="line">&#125;, <span class="keyword">this</span>);</div><div class="line"></div><div class="line"><span class="comment">//在project.json中，jsList数组用于加载js资源</span></div><div class="line"><span class="string">"jsList"</span> : [</div><div class="line">    <span class="string">"src/resource.js"</span>,</div><div class="line">    <span class="string">"src/app.js"</span></div><div class="line">]</div></pre></td></tr></table></figure>
<h2 id="核心概念"><a href="#核心概念" class="headerlink" title="核心概念"></a>核心概念</h2><h3 id="导演"><a href="#导演" class="headerlink" title="导演"></a>导演</h3><p><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945711804166.jpg" alt=""><br>导演cc.director是整个游戏工程的管理者，可以称它为大总管，采用单例模式，也就是说，不管在游戏里面任何一个场景访问它获取到的都是一个对象。<br>它的主要职责是访问和切换游戏场景，访问一些API配置信息，暂停、继续、结束游戏，转换坐标等</p>
<h3 id="场景scene"><a href="#场景scene" class="headerlink" title="场景scene"></a>场景scene</h3><p>场景scene是构成游戏的界面，类似于电影中的场景。继承于node，是层layer的容器。<br>主要分为三类场景。</p>
<ul>
<li>展示类场景:主要展示一些视频、游戏介绍等</li>
<li>选项类场景：主要是主菜单、设置选项等场景</li>
<li>游戏主场景：游戏运行的主要场景，是游戏中最重要的场景<h3 id="层layer"><a href="#层layer" class="headerlink" title="层layer"></a>层layer</h3><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945723879457.jpg" alt=""><br>层是场景的重要组成部分，一个场景由一个或多个层铺叠而成，就如同上图一样，一个游戏开始界面由三层组成，最下面的背景层，中间的精灵层和最上面的菜单层，层相互叠加最终组成最终的游戏界面。<br>需要注意的是，层之间是有顺序的，与此同时，层继承与node，而且是精灵的容器，可以随意存放精灵。</li>
</ul>
<h3 id="精灵"><a href="#精灵" class="headerlink" title="精灵"></a>精灵</h3><p><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945729564081.jpg" alt=""><br>精灵是游戏中极其重要的一部分。比如在坦克大战游戏中，坦克、子弹、增益物品等对象都是精灵，可以说游戏编程其实就是面对精灵编程。在整个游戏中我们会创建很多很多的精灵。</p>
<h3 id="Node究竟是什么？"><a href="#Node究竟是什么？" class="headerlink" title="Node究竟是什么？"></a>Node究竟是什么？</h3><p>从上面的简要介绍中我们可以看出，包括场景，层，精灵等很多元素都继承于Node，那么Node究竟是什么呢？<br><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14945743415606.jpg" alt=""><br>熟悉web开发的人应该对上面这张图应该并不陌生，在web开发中，html是最上层的节点，然后按层依次管理其对应的子节点。与之类似，在cocos2d-x中，节点管理也是采用类似的方式。场景是最外层的容器，类似于html节点，采用树形结构依次管理对应的层和精灵。其中所有的节点均继承自Node，具有Node的所有属性和方法。<br>其中，Node的常用属性和方法如下所示</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">1.重要操作</div><div class="line">    - var node=new cc.Node();//创建节点</div><div class="line">    - node.addChild(childNode,0,123);//子节点，层级，标签</div><div class="line">    - var childNode=node.getChildByTag(123);</div><div class="line">    - node.removeChildByTag(123,true);移除子节点及其相关动作</div><div class="line">    - node.removeChild(childNode,true);移除子节点及其相关动作</div><div class="line"></div><div class="line">    </div><div class="line">2.重要属性</div><div class="line">    - position  node在场景中的实际位置</div><div class="line">    - anchorPoint(0.5,0.5); 锚点，node相对position的比例，也是运动的不动点</div><div class="line">    - anchorPoint=(w1/w,h1/h);</div><div class="line">    </div><div class="line">    node.setPosition(x,y);</div><div class="line">    node.setAnchorPoint(0.5,0.5);</div></pre></td></tr></table></figure>
<h3 id="事件循环与调度"><a href="#事件循环与调度" class="headerlink" title="事件循环与调度"></a>事件循环与调度</h3><p>我们知道，跟拍电影类似 整个过程是由一帧一帧的图片连续播放而成。cocos2d-x游戏工程也是基于不断的刷新重绘页面的原理制作而成的。在整个游戏的过程中，引擎会基于特定的帧率(默认60帧)来不断更新游戏页面。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">//让游戏进行循环</div><div class="line">scene.scheduleUpdate();</div><div class="line"></div><div class="line">update:function(dt)&#123;</div><div class="line">    //执行循环代码</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>正如上面的代码，当某个场景(层)调用<code>scheduleUpdate()</code>这个函数，就会启用工程的刷新机制，会不断调用当前对象对应的<code>update()</code>函数，从而达到更新游戏的目的</p>
<h3 id="标签与菜单"><a href="#标签与菜单" class="headerlink" title="标签与菜单"></a>标签与菜单</h3><p>在游戏中，我们可以看到上面会有各种各样的文字和菜单，比如坦克大战中坦克的数量，杀敌数等数据，以及开始游戏时候的开始游戏，结束游戏等菜单。</p>
<h4 id="标签"><a href="#标签" class="headerlink" title="标签"></a>标签</h4><blockquote>
<p><code>cc.LabelTTF</code>使用系统字体来定义文字</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">var helloLabel=new cc.LabelTFF(&quot;hello world&quot;,&quot;Arial&quot;,38);</div><div class="line"></div><div class="line"></div><div class="line">//如果使用的是第三方字体，需要将字体文件加入resource.js中</div><div class="line">varg_resources=[&#123;</div><div class="line">    type:&quot;font&quot;,</div><div class="line">    name:&quot;Courier New&quot;,</div><div class="line">    srcs:[&quot;res/fonts/Courier New.ttf&quot;]</div><div class="line">&#125;];</div></pre></td></tr></table></figure>
<blockquote>
<p><code>cc.LabelAtlas</code>图片集标签，这种标签的文字是从一个图片集中取出来的，需要额外加载图片集文件，比前者快许多</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">//图片集需要在资源文件中定义</div><div class="line">var helloLabel=new cc.LabelAtlas(&quot;hello world&quot;,res.charmap.png,48,46,&quot;&quot;)</div></pre></td></tr></table></figure>
<blockquote>
<p><code>cc.LabelBMFont</code>使用位图字体来添加字体，它比前两者速度要快得多</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">//资源文件中需要定义字体文件</div><div class="line">var helloLabel=new cc.LabelBMFont(&quot;hello world&quot;,res.BMFont_fnt)</div></pre></td></tr></table></figure>
<h4 id="菜单"><a href="#菜单" class="headerlink" title="菜单"></a>菜单</h4><p>菜单分为三种，分别是文本菜单、精灵菜单、开关菜单</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">//文本菜单</div><div class="line">cc.MenuItemFont.setFontName(&quot;Times New Roman&quot;);</div><div class="line">cc.MenuItemFont.setFontSize(86);</div><div class="line"></div><div class="line">var item1=new cc.MenuItemFont(&quot;Start&quot;,this.menuItem1CallBack,this);</div><div class="line"></div><div class="line">//精灵菜单与图片菜单</div><div class="line">var startSpriteNormal = new cc.Sprite(res.start_up_png);</div><div class="line">var startSpriteSelected = new cc.Sprite(res.start_down_png);</div><div class="line">var startMenuItem = new cc.MenuItemSprite(</div><div class="line">            startSpriteNormal,</div><div class="line">            startSpriteSelected,</div><div class="line">            this.menuItemStartCallback, this);</div><div class="line"></div><div class="line">var settingMenuItem = new cc.MenuItemImage(</div><div class="line">            res.setting_up_png,</div><div class="line">            res.setting_down_png,</div><div class="line">            this.menuItemSettingCallback, this);</div><div class="line">            </div><div class="line">//开关菜单</div><div class="line">var soundOnMenuItem = new cc.MenuItemImage(</div><div class="line">            res.On_png, res.On_png);</div><div class="line">var soundOffMenuItem = new cc.MenuItemImage(</div><div class="line">            res.Off_png, res.Off_png);</div><div class="line">var soundToggleMenuItem = new cc.MenuItemToggle(</div><div class="line">            soundOnMenuItem,</div><div class="line">            soundOffMenuItem,</div><div class="line">            this.menuSoundToggleCallback, this);</div></pre></td></tr></table></figure>
<h3 id="动作与动画"><a href="#动作与动画" class="headerlink" title="动作与动画"></a>动作与动画</h3><h4 id="动作"><a href="#动作" class="headerlink" title="动作"></a>动作</h4><blockquote>
<p>属性变化动作，其中position可以是cc.p(x,y)，也可以是x</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">//直线移动</div><div class="line">- cc.moveTo(duration, position, y); </div><div class="line">- cc.moveBy(duration, position, y);</div><div class="line"></div><div class="line">//跳跃移动，jump为跳跃次数</div><div class="line">cc.jumpTo(duration, position, y, height, jumps); </div><div class="line">cc.jumpBy(duration, position, y, height, jumps);</div><div class="line"></div><div class="line">//贝塞尔曲线运动，cc.BezierTo cc.BezierBy</div><div class="line">var size=cc.winSize;</div><div class="line">var control = [</div><div class="line">    cc.p(0, size.height),</div><div class="line">    cc.p(size.width, size.height),</div><div class="line">    cc.p(size.width, 0)</div><div class="line">];</div><div class="line"></div><div class="line">cc.bezierTo(duration, control); </div><div class="line">cc.bezierBy(duration, control);</div><div class="line"></div><div class="line"></div><div class="line">//cc.ScaleTo cc.ScaleBy缩放</div><div class="line"></div><div class="line">//cc.rotateTo和cc.rotateBy旋转</div></pre></td></tr></table></figure>
<blockquote>
<p>视觉特效动作</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">//淡入淡出  cc.FadeIn cc.FadeOut cc.FadeTo</div><div class="line">var fadeOut = cc.fadeOut(1);</div><div class="line">var fadeIn = cc.fadeIn(1);</div><div class="line">var fadeTo = cc.fadeTo(1, 128);</div><div class="line">node.runAction(cc.sequence(fadeOut, fadeIn, fadeTo));</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">//闪烁 cc.blink,1s时间闪烁10次</div><div class="line">var actionBlink = cc.blink(1, 10); </div><div class="line">node.runAction(actionBlink);</div><div class="line"></div><div class="line"></div><div class="line">//cc.Animation 自定义帧动画 参数:帧对象、间隔时间、循环次数</div><div class="line">var animation = new cc.Animation(frames, delay, loops);</div><div class="line">//实例</div><div class="line">var animation = new cc.Animation(); // 创建动作</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_64_png);</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_128_png);</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_256_png);</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_512_png);</div><div class="line">animation.setDelayPerUnit(0.15); // 设置间隔时间</div><div class="line">animation.setRestoreOriginalFrame(true); // 设置是否回复到第一帧</div><div class="line">var animate = cc.animate(animation); // 通过cc.animate将animation封装成动作 </div><div class="line">node.runAction(animate);</div></pre></td></tr></table></figure>
<blockquote>
<p>复合动作</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">//cc.DelayTime延时动作，只起到延时作用</div><div class="line">cc.delayTime(duration);</div><div class="line"></div><div class="line"></div><div class="line">//cc.Repeat cc.RepeatForever 重复执行</div><div class="line">//用0.5s旋转节点90度 </div><div class="line">var rotate = cc.rotateBy(0.5, -90);</div><div class="line">var repeat = rotate.repeat(4); //  重复4次 </div><div class="line">node.runAction(repeat);</div><div class="line"> </div><div class="line">var rotate = cc.rotateBy(0.5, -90);</div><div class="line">var repeat = rotate.repeatForever();//一直重复</div><div class="line">node.runAction(repeat);</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">//cc.Sequence 有顺序执行动作</div><div class="line">//cc.Spawn同步执行动作</div></pre></td></tr></table></figure>
<h3 id="动画"><a href="#动画" class="headerlink" title="动画"></a>动画</h3><blockquote>
<p>此处主要介绍帧动画，骨骼动画制作较为复杂，此处不做介绍</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">//cc.Animation 自定义帧动画 参数:帧对象、间隔时间、循环次数</div><div class="line">var animation = new cc.Animation(frames, delay, loops);</div><div class="line">//实例</div><div class="line">var animation = new cc.Animation(); // 创建动作</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_64_png);</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_128_png);</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_256_png);</div><div class="line">animation.addSpriteFrameWithFile(res.sh_node_512_png);</div><div class="line">animation.setDelayPerUnit(0.15); // 设置间隔时间</div><div class="line">animation.setRestoreOriginalFrame(true); // 设置是否回复到第一帧</div><div class="line">var animate = cc.animate(animation); // 通过cc.animate将animation封装成动作 </div><div class="line">node.runAction(animate);</div></pre></td></tr></table></figure>
<h3 id="事件"><a href="#事件" class="headerlink" title="事件"></a>事件</h3><p>在cocos2d-x中，事件主要分为鼠标事件，触摸事件，键盘事件和自定义事件等。此处简要介绍以下单点触摸事件和鼠标事件<br><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948319177139.jpg" alt=""><br>如上代码所示，首先检测是不是触摸事件，如果是，则在事件管理器中添加单点触摸事件，并绑定move时的触发函数。如果不是触摸事件，则在事件管理器中添加鼠标事件，并绑定相关触发函数</p>
<h3 id="音频处理"><a href="#音频处理" class="headerlink" title="音频处理"></a>音频处理</h3><blockquote>
<p>在<code>cocos2d-x</code>中，音频控制主要是在<code>cc.audioEngine</code>中控制的，以下是相关的API，直接调用即可。不过需要注意的是，相关音频必须在res目录中且被代码引入</p>
</blockquote>
<p><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948325815436.jpg" alt=""></p>
<h3 id="数据存储"><a href="#数据存储" class="headerlink" title="数据存储"></a>数据存储</h3><p>我们都知道，除了那种特别简单的游戏，一般都是需要存储游戏数据。在cocos2d-x阴影中当然也不例外。通常来说，如果游戏数据存储在本地，我们一般采用localStorage方法，将数据存储在本地。<br>如果数据需要存储在服务端，自然而然要存放在数据库中，在客户端与服务端交换数据时候可以采用json形式<br><img src="http://oprp8vydn.bkt.clouddn.com/2017-05-15-14948330435333.jpg" alt=""></p>
<h2 id="相关拓展"><a href="#相关拓展" class="headerlink" title="相关拓展"></a>相关拓展</h2><p>开发一个游戏当然远远不止上面展示的那么简单，里面牵扯到的内容还有很多很多，以下简要介绍一下cocos2d-x开发游戏需要了解到的技术点，当然，我所提到的只是九牛一毛，开发游戏水很深，需要大家持续不断的去学习，去努力</p>
<ul>
<li>物理引擎</li>
<li>性能优化</li>
<li>3D特性</li>
<li>网络通信</li>
<li>多平台移植</li>
<li>屏幕适配</li>
<li>瓦片地图</li>
<li>粒子系统</li>
<li>骨骼动画</li>
</ul>
</span>
      
    </div>

    <footer class="post-footer">

      
        <div class="post-nav">
          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/05/24/LexHuang/Inline-Caches/" rel="prev">【译】用JavaScript解释JavaScript虚拟机-内联缓存（inline caches）</a>
            
          </div>

          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/05/04/y8n/understand-virtual-dom/" rel="next">理解Virtual DOM</a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>

 </div>

        

        
          <div class="comments" id="comments">
            <div id="SOHUCS" sid="" ></div>
          </div>
        
      </div>

      
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <a href="https://github.com/xgfe" target="_blank"><img class="site-author-image" src="http://p0.meituan.net/xgfe/082a9624ba5ae8602150a2d43968463e49348.png" alt="xgfe" itemprop="image"/></a>
          <p class="site-author-name" itemprop="name">xgfe</p>
        </div>
        <p class="site-description motion-element" itemprop="description">xgfe's blog. 鲜果前端的技术博客，鲜果前端研发部官方博客。前端基础技术研究：html, html5, javascript, css, css3；前端框架研究：angularJs, react, react native.</p>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">89</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          <div class="site-state-item site-state-categories">
            
              <span class="site-state-item-count">37</span>
              <span class="site-state-item-name">作者</span>
              
          </div>

          <div class="site-state-item site-state-tags">
            <a href="/tags">
              <span class="site-state-item-count">131</span>
              <span class="site-state-item-name">标签</span>
              </a>
          </div>

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" target="_blank" rel="alternate">
              <i class="menu-item-icon icon-next-feed"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/xgfe" target="_blank">GitHub</a>
              </span>
            
          
        </div>

        
        

        <div class="links-of-author motion-element">
          
        </div>

      </section>

      
        <section class="post-toc-wrap sidebar-panel-active">
          <div class="post-toc-indicator-top post-toc-indicator"></div>
          <div class="post-toc">
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#环境搭建"><span class="nav-number">1.</span> <span class="nav-text">环境搭建</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#概述"><span class="nav-number">1.1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设计目标"><span class="nav-number">1.2.</span> <span class="nav-text">设计目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#开发环境搭建"><span class="nav-number">1.3.</span> <span class="nav-text">开发环境搭建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一个简单的实例-helloWorld"><span class="nav-number">1.4.</span> <span class="nav-text">一个简单的实例 helloWorld</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#核心概念"><span class="nav-number">2.</span> <span class="nav-text">核心概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#导演"><span class="nav-number">2.1.</span> <span class="nav-text">导演</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#场景scene"><span class="nav-number">2.2.</span> <span class="nav-text">场景scene</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#层layer"><span class="nav-number">2.3.</span> <span class="nav-text">层layer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#精灵"><span class="nav-number">2.4.</span> <span class="nav-text">精灵</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Node究竟是什么？"><span class="nav-number">2.5.</span> <span class="nav-text">Node究竟是什么？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#事件循环与调度"><span class="nav-number">2.6.</span> <span class="nav-text">事件循环与调度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#标签与菜单"><span class="nav-number">2.7.</span> <span class="nav-text">标签与菜单</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#标签"><span class="nav-number">2.7.1.</span> <span class="nav-text">标签</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#菜单"><span class="nav-number">2.7.2.</span> <span class="nav-text">菜单</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动作与动画"><span class="nav-number">2.8.</span> <span class="nav-text">动作与动画</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#动作"><span class="nav-number">2.8.1.</span> <span class="nav-text">动作</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动画"><span class="nav-number">2.9.</span> <span class="nav-text">动画</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#事件"><span class="nav-number">2.10.</span> <span class="nav-text">事件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#音频处理"><span class="nav-number">2.11.</span> <span class="nav-text">音频处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据存储"><span class="nav-number">2.12.</span> <span class="nav-text">数据存储</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#相关拓展"><span class="nav-number">3.</span> <span class="nav-text">相关拓展</span></a></li></ol></div>
            
          </div>
          <div class="post-toc-indicator-bottom post-toc-indicator"></div>
        </section>
      

    </div>
  </aside>


    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner"> <div class="copyright" >
  
  &copy; &nbsp; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="icon-next-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xgfe</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" target="_blank" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


 </div>
    </footer>

    <div class="back-to-top"></div>
  </div>

  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  
  
    <script type="text/javascript"> 
(function(){ 
var appid = 'cysWiXvkm'; 
var conf = 'prod_fc970dbe85103c7a79b2c4f3dc7fb190'; 
var width = window.innerWidth || document.documentElement.clientWidth; 
if (width < 960) { 
window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="http://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("http://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})}); } })(); </script>
    

  


  
  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js"></script>
  <script type="text/javascript" src="/js/fancy-box.js?v=0.4.5.1"></script>


  <script type="text/javascript" src="/js/helpers.js?v=0.4.5.1"></script>
  

  <script type="text/javascript" src="/vendors/velocity/velocity.min.js"></script>
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js"></script>

  <script type="text/javascript" src="/js/motion_global.js?v=0.4.5.1" id="motion.global"></script>




  <script type="text/javascript" src="/js/nav-toggle.js?v=0.4.5.1"></script>
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  
<script type="text/javascript" src="/js/bootstrap.scrollspy.js?v=0.4.5.1" id="bootstrap.scrollspy.custom"></script>


<script type="text/javascript" id="sidebar.toc.highlight">
  $(document).ready(function () {
    var tocSelector = '.post-toc';
    var $tocSelector = $(tocSelector);
    var activeCurrentSelector = '.active-current';

    $tocSelector
      .on('activate.bs.scrollspy', function () {
        var $currentActiveElement = $(tocSelector + ' .active').last();

        removeCurrentActiveClass();
        $currentActiveElement.addClass('active-current');

        $tocSelector[0].scrollTop = $currentActiveElement.position().top;
      })
      .on('clear.bs.scrollspy', function () {
        removeCurrentActiveClass();
      });

    function removeCurrentActiveClass () {
      $(tocSelector + ' ' + activeCurrentSelector)
        .removeClass(activeCurrentSelector.substring(1));
    }

    function processTOC () {
      getTOCMaxHeight();
      toggleTOCOverflowIndicators();
    }

    function getTOCMaxHeight () {
      var height = $('.sidebar').height() -
                   $tocSelector.position().top -
                   $('.post-toc-indicator-bottom').height();

      $tocSelector.css('height', height);

      return height;
    }

    function toggleTOCOverflowIndicators () {
      tocOverflowIndicator(
        '.post-toc-indicator-top',
        $tocSelector.scrollTop() > 0 ? 'show' : 'hide'
      );

      tocOverflowIndicator(
        '.post-toc-indicator-bottom',
        $tocSelector.scrollTop() >= $tocSelector.find('ol').height() - $tocSelector.height() ? 'hide' : 'show'
      )
    }

    $(document).on('sidebar.motion.complete', function () {
      processTOC();
    });

    $('body').scrollspy({ target: tocSelector });
    $(window).on('resize', function () {
      if ( $('.sidebar').hasClass('sidebar-active') ) {
        processTOC();
      }
    });

    onScroll($tocSelector);

    function onScroll (element) {
      element.on('mousewheel DOMMouseScroll', function (event) {
          var oe = event.originalEvent;
          var delta = oe.wheelDelta || -oe.detail;

          this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
          event.preventDefault();

          toggleTOCOverflowIndicators();
      });
    }

    function tocOverflowIndicator (indicator, action) {
      var $indicator = $(indicator);
      var opacity = action === 'show' ? 0.4 : 0;
      $indicator.velocity ?
        $indicator.velocity('stop').velocity({
          opacity: opacity
        }, { duration: 100 }) :
        $indicator.stop().animate({
          opacity: opacity
        }, 100);
    }

  });
</script>

<script type="text/javascript" id="sidebar.nav">
  $(document).ready(function () {
    var html = $('html');
    var TAB_ANIMATE_DURATION = 200;
    var hasVelocity = $.isFunction(html.velocity);

    $('.sidebar-nav li').on('click', function () {
      var item = $(this);
      var activeTabClassName = 'sidebar-nav-active';
      var activePanelClassName = 'sidebar-panel-active';
      if (item.hasClass(activeTabClassName)) {
        return;
      }

      var currentTarget = $('.' + activePanelClassName);
      var target = $('.' + item.data('target'));

      hasVelocity ?
        currentTarget.velocity('transition.slideUpOut', TAB_ANIMATE_DURATION, function () {
          target
            .velocity('stop')
            .velocity('transition.slideDownIn', TAB_ANIMATE_DURATION)
            .addClass(activePanelClassName);
        }) :
        currentTarget.animate({ opacity: 0 }, TAB_ANIMATE_DURATION, function () {
          currentTarget.hide();
          target
            .stop()
            .css({'opacity': 0, 'display': 'block'})
            .animate({ opacity: 1 }, TAB_ANIMATE_DURATION, function () {
              currentTarget.removeClass(activePanelClassName);
              target.addClass(activePanelClassName);
            });
        });

      item.siblings().removeClass(activeTabClassName);
      item.addClass(activeTabClassName);
    });

    $('.post-toc a').on('click', function (e) {
      e.preventDefault();
      var targetSelector = escapeSelector(this.getAttribute('href'));
      var offset = $(targetSelector).offset().top;
      hasVelocity ?
        html.velocity('stop').velocity('scroll', {
          offset: offset  + 'px',
          mobileHA: false
        }) :
        $('html, body').stop().animate({
          scrollTop: offset
        }, 500);
    });

    // Expand sidebar on post detail page by default, when post has a toc.
    var $tocContent = $('.post-toc-content');
    if (isDesktop() && CONFIG.sidebar === 'post') {
      if ($tocContent.length > 0 && $tocContent.html().trim().length > 0) {
        displaySidebar();
      }
    }
  });
</script>



  <script type="text/javascript">
    $(document).ready(function () {
      if (CONFIG.sidebar === 'always') {
        displaySidebar();
      }
      if (isMobile()) {
        FastClick.attach(document.body);
      }
    });
  </script>

  

  
  

  
  <script type="text/javascript" src="/js/lazyload.js"></script>
  <script type="text/javascript">
    $(function () {
      $("#posts").find('img').lazyload({
        placeholder: "/images/loading.gif",
        effect: "fadeIn"
      });
    });
  </script>

  <!-- google search, added by felix -->
  <script>
      $('#gg-form').on('submit', function(e) {
        var keyword = $.trim($(this).find('#gg-search-input').val());
        if (keyword) {
          location.href = 'https://www.google.com.hk/?gfe_rd=cr&ei=hXw8VpjtHuLC8AeSuIjQAg&gws_rd=ssl#safe=strict&q='+encodeURIComponent(keyword)+'+site:xgfe.github.io';
        }
        return false;
      });
  </script>
  <!-- baidu 站长自动推送 -->
  <script>
  (function(){
      var bp = document.createElement('script');
      bp.src = '//push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>
</body>
</html>
